<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/12 17:33
 * @version
 */
namespace App;

use App\Models\Configure;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MobileRecharge
{
    const API_HOST_ONLINEORDER = 'http://op.juhe.cn/ofpay/mobile/onlineorder';

    const API_HOST_ORDER_STATUS = 'http://op.juhe.cn/ofpay/mobile/ordersta';

    public static function onlineOrder($phoneno,$cardnum,$orderid)
    {
        $cardnum = intval($cardnum);
        $params = [
            'phoneno'=>$phoneno,'cardnum'=>$cardnum,'orderid'=>$orderid,'key'=>config('juhe.mobile_appkey')
        ];
        $openid = config('juhe.openid');
        $params['sign'] = md5($openid . $params['key'] . $phoneno . $cardnum . $orderid);

        $client = new Client();
        $response = $client->request('POST',self::API_HOST_ONLINEORDER,['form_params'=>$params]);
        if($response->getStatusCode() == 200){
            $result = json_decode($response->getBody(), true);
            if ($result['error_code'] == 0) {
                //充值成功

                return true;
            }
            Log::error($result['error_code'],$params);
        }

        return false;
    }

    public static function syncOrderStatus($orderid)
    {
        $params = [
            'orderid'=>$orderid,'key'=>config('juhe.mobile_appkey')
        ];

        $client = new Client();
        $response = $client->request('POST',self::API_HOST_ORDER_STATUS,['form_params'=>$params]);
        if($response->getStatusCode() == 200){
            $result = json_decode($response->getBody(), true);
            if ($result['error_code'] == 0 && $result['result']['game_state'] == 1) {
                //充值成功

                return true;
            }else{
                Log::info('mobile recharge:',$result);
            }
        }
        return false;
    }
}